<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px" size="small">
    <el-form-item label="paramKey" prop="paramKey">
      <el-input v-model="dataForm.paramKey" placeholder="key"></el-input>
    </el-form-item>
    <el-form-item label="paramValue" prop="paramValue">
      <el-input v-model="dataForm.paramValue" placeholder="value"></el-input>
    </el-form-item>
      <el-form-item label="paramExt" prop="paramExt">
        <el-input v-model="dataForm.paramExt" placeholder="paramExt"></el-input>
      </el-form-item>
      <el-form-item label="paramExt1" prop="paramExt1">
        <el-input v-model="dataForm.paramExt1" placeholder="paramExt1"></el-input>
      </el-form-item>
      <el-form-item label="parentParamKey" prop="parentParamKey">
        <el-input v-model="dataForm.parentParamKey" placeholder="parentParamKey"></el-input>
      </el-form-item>
    <el-form-item label="分类" prop="paramGroup">
      <el-input v-model="dataForm.paramGroup" placeholder="分类"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-radio v-model="dataForm.status" label="1">显示</el-radio>
      <el-radio v-model="dataForm.status" label="0">隐藏</el-radio>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false" size="small">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" size="small">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          paramKey: '',
          paramValue: '',
          paramExt: '',
          paramGroup:'',
          parentParamKey: '',
          paramExt1: '',
          status: '1',
          remark: ''
        },
        dataRule: {
          paramKey: [
            { required: true, message: 'key不能为空', trigger: 'blur' }
          ],
          paramValue: [
            { required: true, message: 'value不能为空', trigger: 'blur' }
          ],
          paramGroup: [
            { required: true, message: '分类不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态   0：隐藏   1：显示不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/sys/sysstaticdata/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.paramKey = data.sysstaticdata.paramKey
                this.dataForm.paramValue = data.sysstaticdata.paramValue
              this.dataForm.paramExt = data.sysstaticdata.paramExt
                this.dataForm.paramGroup = data.sysstaticdata.paramGroup
                this.dataForm.parentParamKey = data.sysstaticdata.parentParamKey
              this.dataForm.paramExt1 = data.sysstaticdata.paramExt1
                this.dataForm.status = data.sysstaticdata.status+''
                this.dataForm.remark = data.sysstaticdata.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/sys/sysstaticdata/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'paramKey': this.dataForm.paramKey,
                'paramValue': this.dataForm.paramValue,
                'paramExt': this.dataForm.paramExt,
                'paramGroup': this.dataForm.paramGroup,
                'parentParamKey': this.dataForm.parentParamKey,
                'paramExt1': this.dataForm.paramExt1,
                'status': this.dataForm.status,
                'remark': this.dataForm.remark
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
